import { observable, action } from 'mobx-miniprogram'
import { getThemeConfig, getProtocol } from "@src/api/base";
import $color from "@src/common/color";
import $cache from "@src/common/wxCache";
import config from "@src/config";
import dianpufenlei from '@src/store/dianpufenlei'
import { isECDS } from "@src/api/invoice";
export default observable({
  // 数据字段
  isInit: false,
  businessName: null,
  businessLogo: null,
  inAudit: false,
  themeCode: '',
  cardFields: {},
  themeColor: null,//background
  themeTitleColor: null,
  titleColor: null,
  homePage: null,
  tabBar: [],
  splashScreen: null,
  isiPhoneX: false,
  myPageBg: null,
  homeBanners: [],
  myPageBanners: [],
  features: [],
  services: [],
  permFeatures: [],
  perms: {},
  indexPageScene: {
    scene: '',
    receiveWay: ''
  },
  barHeight: null,
  statusBarHeight: null,
  navBarHeight: null,
  protocol: null,
  isECDS: false,
  // storeId: 12,
  // actions
  themeConfig: action(function (cb) {
    let themeConfig = $cache.get("themeConfig-" + config.tpl)
    if (themeConfig) {
      this.init(themeConfig, 'theme');
    }
    getThemeConfig().then(async res => {
      if (res.code === 0) {
        $cache.setSync("themeConfig-" + config.tpl, res.data, 60 * 60 * 24 * 15);
        const { deptId } = res.data
        try {
          this.init({...res.data});
          const protocol =  await getProtocol({deptId, userAgreementName: '用户使用协议与隐私政策'})
          protocol.data.code === 200 && (this.protocol = protocol.data.data)
          cb && typeof cb === 'function' && cb(res.data)
        } catch (error) {
          console.error(error)
        }
      } else {
        console.error("请求失败", error)
      }
    }).catch(error => {
      console.error("请求失败", error)
    })
    wx.getSystemInfo({
      success: res => {
        if (res.safeArea.top > 20) { this.isiPhoneX = true }
      }
    })
  }),
  init: action(function (data, type) {
    console.log(data.homePage)
    this.businessName = data.businessName;
    this.businessLogo = data.businessLogo;
    this.inAudit = data.inAudit;
    this.themeCode = data.themeCode;
    this.themeColor = data.color;
    if (data.color) {
      this.themeTitleColor = $color.getTitleColor(data.color);
    } else {
      this.themeTitleColor = $color.getTitleColor('#63b359');
    }
    if (!type && data.homePage) {
      this.homePage = data.homePage
    }
    this.splashScreen = data.splashScreen;
    this.myPageBg = data.myPageBg;
    this.homeBanners = data.homeBanners;
    this.myPageBanners = data.myPageBanners;
    this.features = data.features;
    this.services = data.services;
    this.permFeatures = data.permFeatures;
    //增加了一个状态栏高度
    const systemInfo = wx.getSystemInfoSync();
    let navBarHeight = 0;
    if (/ios/i.test(systemInfo.platform)) {
      navBarHeight = 44;
    } else if (systemInfo.platform === "devtools") {
      navBarHeight = 44;
    } else {
      navBarHeight = 48;
    }
    this.barHeight = navBarHeight + systemInfo.statusBarHeight;
    this.statusBarHeight = systemInfo.statusBarHeight;
    this.navBarHeight = navBarHeight;
    if (config.tpl === 'default') {
      this.tabBar = data.tabBar;
      let cardFields = {};
      for (let i = 0; i < data.cardFields.length; i++) {
        cardFields[data.cardFields[i]] = true;
      }
      this.cardFields = cardFields;
    } else {
      this.tabBar = data.tabBarList;
    }
    let perms = {}
    for (let i = 0; i < data.permFeatures.length; i++) {
      perms[data.permFeatures[i]] = true;
    };
    this.perms = perms;

  }),
  initReady: action(function () {
    this.isInit = true;
  }),
  setIndexPageScene: action(function (scene, receiveWay) {
    this.indexPageScene.scene = scene;
    this.indexPageScene.receiveWay = receiveWay;
  }),
  setECDSStatus: action(function () {
    isECDS().then(res => {if (res.code === 0) return this.isECDS = res.data.RECEIPT_STATUS === 1 ? true : false })
  })
})


